package com.xinbochuang.machine.admin.controller.macroom;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xinbochuang.machine.admin.domain.macRoom.EquipmentType;
import com.xinbochuang.machine.admin.dto.AesRequestBean;
import com.xinbochuang.machine.common.utils.AESUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.xinbochuang.machine.admin.domain.macRoom.EquCabinet;
import com.xinbochuang.machine.admin.domain.vo.CabinetSubBean;
import com.xinbochuang.machine.admin.service.macRoom.IEquCabinetService;
import com.xinbochuang.machine.admin.service.macRoom.IMacRoomService;
import com.xinbochuang.machine.common.controller.BaseController;
import com.xinbochuang.machine.common.domain.TableDataInfo;
import com.xinbochuang.machine.framework.web.domain.AjaxResult;

/**
 * @author 杜文博
 * @date 2020-09-08
 */
@RestController
@RequestMapping("/equ-cabinet")
public class EquCabinetController extends BaseController {

    @Autowired
    IEquCabinetService equCabinetService;

    @Autowired
    IMacRoomService macRoomService;

    /**
     * 通过机柜姓名查询机柜
     *
     * @param equCabinet
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo getEquDetailList(EquCabinet equCabinet) {
        return getDataTable(equCabinetService.findEquCabinetListByName(equCabinet));
    }

    /**
     * 根据macId查询机柜详情
     *
     * @param response
     * @param macId
     * @return
     */
    @GetMapping("/cabinetList")
    public AjaxResult getCabinetList(HttpServletResponse response, @RequestParam(value = "macId")
    String macId) {
        List<EquCabinet> list = macRoomService.finEquCabinetListByMacId(macId);
        return AjaxResult.data(list);
    }

    /**
     * 删除机柜
     *
     * @param response
     * @param cabinetId
     * @return
     */
    @GetMapping("/delete")
    public AjaxResult deleteMacRoomById(HttpServletResponse response, @RequestParam(value = "cabinetId")
    String cabinetId) {
        EquCabinet equCabinet = new EquCabinet();
        equCabinet.setUuid(cabinetId);
        equCabinet.setDeleteFlag(1);
        AjaxResult ajaxResult = new AjaxResult();
        if (equCabinetService.updateById(equCabinet)) {
            ajaxResult = AjaxResult.ok();
            ajaxResult.put("result", equCabinetService.updateById(equCabinet));
            return ajaxResult;
        } else {
            ajaxResult = AjaxResult.error();
        }
        return ajaxResult;
    }

    /**
     * 删除机柜
     *
     * @param cabinetId
     * @return
     */
    @GetMapping("/getDetialById")
    public AjaxResult getDetialById(@RequestParam(value = "cabinetId")
                                    String cabinetId) {
        EquCabinet equCabinet = new EquCabinet();
        equCabinet.setUuid(cabinetId);
        AjaxResult ajaxResult = new AjaxResult();

        ajaxResult = AjaxResult.ok();
        Map<String, Object> result = equCabinetService.getDetialById(equCabinet);
        ajaxResult.put("result", result);
        return ajaxResult;
    }

    @GetMapping("/getDetialByIdN")
    public AjaxResult getDetialByIdN(@RequestParam(value = "cabinetId")
                                     String cabinetId) {
        EquCabinet equCabinet = new EquCabinet();
        equCabinet.setUuid(cabinetId);
        AjaxResult ajaxResult = new AjaxResult();

        ajaxResult = AjaxResult.ok();
        Map<String, Object> result = equCabinetService.getDetialById(equCabinet);
        ajaxResult.put("result", AESUtils.encode(JSON.toJSONStringWithDateFormat(result,"yyyy-MM-dd HH:mm:ss")));
        return ajaxResult;
    }

    /**
     * 删除机柜
     *
     * @param cabinetId
     * @return
     */
    @GetMapping("/getDetialByIdApp")
    public AjaxResult getDetialByIdApp(@RequestParam(value = "cabinetId")
                                       String cabinetId, @RequestParam(value = "planId")
                                       String planId) {
        EquCabinet equCabinet = new EquCabinet();
        equCabinet.setUuid(cabinetId);
        AjaxResult ajaxResult = new AjaxResult();

        ajaxResult = AjaxResult.ok();
        Map<String, Object> result = equCabinetService.getDetialById(equCabinet,planId);
        ajaxResult.put("result", result);
        return ajaxResult;
    }

    @GetMapping("/getDetialByIdAppN")
    public AjaxResult getDetialByIdAppN(@RequestParam(value = "cabinetId")
                                        String cabinetId, @RequestParam(value = "planId")
                                        String planId) {
        EquCabinet equCabinet = new EquCabinet();
        equCabinet.setUuid(cabinetId);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult = AjaxResult.ok();
        Map<String, Object> result = equCabinetService.getDetialById(equCabinet,planId);
        ajaxResult.put("result", AESUtils.encode(JSON.toJSONStringWithDateFormat(result,"yyyy-MM-dd HH:mm:ss")));
        return ajaxResult;
    }

    /**
     * app机柜数据提交
     *
     * @return
     */
    @PostMapping("/api/edit")
    public AjaxResult appEdit(@RequestBody CabinetSubBean cabinetSubBean) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult = AjaxResult.ok();
        equCabinetService.contractInsert(cabinetSubBean);
        return ajaxResult;
    }

    /**
     * app机柜数据提交
     *
     * @return
     */
    @PostMapping("/api/editN")
    public AjaxResult appEdit(@RequestBody AesRequestBean cabinetSubBean) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult = AjaxResult.ok();
        equCabinetService.contractInsert(JSONObject.parseObject(AESUtils.decode(cabinetSubBean.getData()),CabinetSubBean.class));
        return ajaxResult;
    }

    /**
     * app机柜数据提交
     *
     * @return
     */
    @PostMapping("/api/editNoOrder")
    public AjaxResult editNoOrder(@RequestBody CabinetSubBean cabinetSubBean) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult = AjaxResult.ok();
        equCabinetService.editNoOrder(cabinetSubBean);
        return ajaxResult;
    }

    /**
     * app机柜数据提交
     *
     * @return
     */
    @PostMapping("/api/editNoOrderN")
    public AjaxResult editNoOrderN(@RequestBody AesRequestBean aesRequestBean) {
        String decode = AESUtils.decode(aesRequestBean.getData());
        CabinetSubBean cabinetSubBean = JSON.parseObject(decode,CabinetSubBean.class);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult = AjaxResult.ok();
        equCabinetService.editNoOrder(cabinetSubBean);
        return ajaxResult;
    }

    /**
     * app机柜数据提交
     *
     * @return
     */
    @GetMapping("/api/getAllType")
    public AjaxResult getAllType(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult = AjaxResult.ok();
        List<EquipmentType> equipmentTypes = equCabinetService.getAllType();
        ajaxResult.put("result", equipmentTypes);
        return ajaxResult;
    }

    /**
     * app机柜数据提交
     *
     * @return
     */
    @GetMapping("/api/getAllTypeN")
    public AjaxResult getAllTypeN(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult = AjaxResult.ok();
        List<EquipmentType> equipmentTypes = equCabinetService.getAllType();
        ajaxResult.put("result",AESUtils.encode(JSON.toJSONStringWithDateFormat( equipmentTypes,"yyyy-MM-dd HH:mm:ss")));
        return ajaxResult;
    }
}

